﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetListRelationCls
    {
        public string AssetListRelationId;
        public string frkRelAssetListId;
        public string ClassCode;
        public string Subject;
    }


    public class AssetListRelationParser
    {
        public static AssetListRelationCls CreateInstance()
        {
            //tao 1 class trang....
            AssetListRelationCls oAssetListRelationCls = new AssetListRelationCls();
            return oAssetListRelationCls;
        }

        public static AssetListRelationCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetListRelationCls oAssetListRelationCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetListRelationCls;
        }


        public static AssetListRelationCls ParseFromDataRow(DataRow dr)
        {
            AssetListRelationCls oAssetListRelationCls = new AssetListRelationCls();

            if (dr["AssetListRelationId"] != DBNull.Value)
            {
                oAssetListRelationCls.AssetListRelationId = (string)dr["AssetListRelationId"];
            }
            if (dr["frkRelAssetListId"] != DBNull.Value)
            {
                oAssetListRelationCls.frkRelAssetListId = (string)dr["frkRelAssetListId"];
            }
            if (dr["ClassCode"] != DBNull.Value)
            {
                oAssetListRelationCls.ClassCode = (string)dr["ClassCode"];
            }
            if (dr["Subject"] != DBNull.Value)
            {
                oAssetListRelationCls.Subject = (string)dr["Url"];
            }

            return oAssetListRelationCls;
        }

        public static AssetListRelationCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetListRelationCls[] AssetListRelationCls = new AssetListRelationCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetListRelationCls[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetListRelationCls;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("AssetListRelation");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetListRelationId");
            ds.Tables["Info"].Columns.Add("frkRelAssetListId");
            ds.Tables["Info"].Columns.Add("ClassCode");
            ds.Tables["Info"].Columns.Add("Subject");

            return ds;
        }

        public static string GetXml(AssetListRelationCls oAssetListRelationCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetListRelationCls.AssetListRelationId,
                        oAssetListRelationCls.frkRelAssetListId,
                        oAssetListRelationCls.ClassCode,
                        oAssetListRelationCls.Subject,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetListRelationCls oAssetListRelation = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetListRelation);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetListRelationCls[] AssetListRelationCls = new AssetListRelationCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetListRelationCls[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetListRelationCls);
            return Xml;
        }


        public static string GetXml(AssetListRelationCls[] AssetListRelationCls)
        {
            if (AssetListRelationCls == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetListRelationCls = new AssetListRelationCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetListRelationCls.Length; iIndex++)
            {
                AssetListRelationCls oAssetListRelationCls = AssetListRelationCls[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                        oAssetListRelationCls.AssetListRelationId,
                        oAssetListRelationCls.frkRelAssetListId,
                        oAssetListRelationCls.ClassCode,
                        oAssetListRelationCls.Subject,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetListRelationCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetListRelationCls[0];
            }
            AssetListRelationCls[] AssetListRelationCls = new AssetListRelationCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetListRelationCls[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetListRelationCls;
        }
    }
}
